#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <index.h>
#include <QByteArray>
#include <QMouseEvent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("郑州司徒末的若依");
    this->setWindowIcon(QIcon(":/image/res/stm.jpg"));
    this->setFixedSize(this->width(),this->height());
    captchaImage();
    ui->label_code->installEventFilter(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return){
        on_btn_login_clicked();
    }
}

void MainWindow::on_btn_login_clicked()
{
    ui->btn_login->setEnabled(0);
    username = ui->lineEdit_username->text();
    pwd = ui->lineEdit_pwd->text();
    if(username.isEmpty() || pwd.isEmpty()){
        QMessageBox::information(0,"提示","不能为空");
        return;
    }
    if(ui->widget_3->isVisible()){
        if(ui->lineEdit_code->text().isEmpty()){
            ui->lineEdit_code->setStyleSheet("border:1px solid red;");
            ui->btn_login->setEnabled(1);
            return;
        }
        ui->lineEdit_code->setStyleSheet("");
        code = ui->lineEdit_code->text();
    }
    utils.load();
    QNetworkReply* reply = userApi.login(username,pwd,code,uuid);
    ui->btn_login->setEnabled(1);
    QByteArray bytes = reply->readAll();
    if(bytes.isEmpty()){
        QMessageBox::information(0,"提示","服务器连接失败");
        return;
    }
    QJsonDocument document = QJsonDocument::fromJson(bytes);
    QJsonObject obj = document.object();
    if(200 != obj.value("code").toInt()){
        captchaImage();
        QMessageBox::information(0,"提示",obj.value("msg").toString());
        return;
    }
    QVariant v;
    v.setValue(obj.value("token"));
    qApp->setProperty("token",v);
//  QMessageBox::information(this,"提示",obj.value("msg").toString());
    this->hide();
    Index *index = new Index();
    index->show();
    connect(index,&Index::logout,this,[=](){
        captchaImage();
        this->show();
    });
    connect(&utils,&Utils::back401,this,[=](){
        captchaImage();
        qDebug()<<2;
        this->show();
    });
}

void MainWindow::captchaImage()
{
    QNetworkReply *r = userApi.captchaImage();
    QByteArray bytes = r->readAll();
    if(bytes.isEmpty()){
        QMessageBox::information(0,"提示","服务器连接失败");
        return;
    }
    QJsonDocument document = QJsonDocument::fromJson(bytes);
    QJsonObject obj = document.object();
    if(obj.value("code").toInt() != 200){
        QMessageBox::information(0,"提示",obj.value("msg").toString());
        return;
    }
    captchaEnabled = obj.value("captchaEnabled").toBool();
    if(captchaEnabled == 0){
        ui->widget_3->hide();
        ui->label->hide();
        return;
    }
    ui->widget_3->show();
    ui->label->show();
    uuid = obj.value("uuid").toString();
    img = obj.value("img").toString();
    loadCodeImg();
}

void MainWindow::loadCodeImg()
{
    QNetworkReply *r = userApi.codeImg("data:image/gif;base64," + img);
    QPixmap m;
    m.loadFromData(r->readAll());
    ui->label_code->setPixmap(m);
}

void MainWindow::on_lineEdit_code_textChanged(const QString &arg1)
{
    ui->lineEdit_code->setStyleSheet("background-color: qradialgradient(spread:pad, "
                                     "cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba"
                                     "(255, 255, 255, 255), stop:0.1 rgba(255, 255, 255, 255),"
                                     " stop:0.2 rgba(255, 176, 176, 167), stop:0.3 rgba"
                                     "(255, 151, 151, 92), stop:0.4 rgba(255, 125, 125, 51), "
                                     "stop:0.5 rgba(255, 76, 76, 205), stop:0.52 rgba"
                                     "(255, 76, 76, 205), stop:0.6 rgba(255, 180, 180, 84)"
                                     ", stop:1 rgba(255, 255, 255, 0));color: rgb(255, 255, 255)"
                                     ";font: 14pt Microsoft YaHei UI;");
}

//事件过滤
bool MainWindow::eventFilter(QObject *obj, QEvent *event){
    //mouse button pressed
    if (event->type() == QEvent::MouseButtonPress){
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent->button() == Qt::LeftButton){
            if(obj == ui->label_code){
                captchaImage();
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }else{
        return QObject::eventFilter(obj, event);
    }
}
